package com.isky.visual.proj.controller;


import com.isky.visual.proj.entity.Project;
import com.isky.visual.proj.entity.vo.*;
import com.isky.visual.proj.service.IPanelGroupService;
import com.isky.visual.proj.service.IProjectService;
import com.isky.visual.result.CodeMsg;
import com.isky.visual.result.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 项目分组表 前端控制器
 * </p>
 *
 * @author wudl
 * @since 2020-04-28
 */
@Api(tags = {"设计器组模块"})
@RestController
@RequestMapping("/proj/projectgroup")
public class PanelGroupController {

    @Autowired
    IPanelGroupService panelGroupService;

    @Autowired
    IProjectService projectService;

//    @PostMapping(value = "/getUserAllProject")
//    @ApiOperation("取所有的项目")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "type", value = "仪表盘组类型  1 个人公共 2 个人 ", required = true, dataType = "Long", paramType = "query")
//    })
//    public ResultVo<List<ProjectVisualVO>> getUserAllProject(@RequestParam(name = "type", required = true) Integer type) {
//
//        List<ProjectVisualVO> allPublishVisual = panelGroupService.getAllPublishVisual(type);
//        return ResultVo.success(allPublishVisual);
//    }

    @PostMapping(value = "/getGroupDashboard")
    @ApiOperation("获取项目分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "仪表盘组类型  1 公共 2 个人 ", required = true, dataType = "Long", paramType = "query")
    })
    public ResultVo<List<ProjectVisualVO>> getGroupDashboard(@RequestParam(name = "type", required = true) Integer type) {
        List<ProjectVisualVO> groupDashboards = panelGroupService.getGroupDashboard(type);
        return ResultVo.success(packageProject(type,groupDashboards));
    }

    private List<ProjectVisualVO> packageProject(Integer type, List<ProjectVisualVO> groupDashboards){

        //拿到根目录，封装跟目录下的
        if(groupDashboards != null && !groupDashboards.isEmpty()){
            for (ProjectVisualVO groupDashboard : groupDashboards) {
                if (groupDashboard.getType() != null) {
                    continue;
                }
                groupDashboard.setType(1);

                List<ProjectVisualVO> childen = groupDashboard.getChilden();
                if(childen == null){
                    childen= new ArrayList<>();
                    groupDashboard.setChilden(childen);
                }
                packageProject(type,childen);

                ProjectPageParam projectParam = new ProjectPageParam();
                projectParam.setGroupId(groupDashboard.getId());
                List<Project> projectList = null;
                if (Objects.equals(type, 2)) {
                    projectList = projectService.getProjectListPage(projectParam);
                } else {
                    projectList = projectService.queryShareProjectList(projectParam);
                }

                if(projectList != null && !projectList.isEmpty()){
                    for(Project project : projectList){
                        ProjectVisualVO projectVisualVO = new ProjectVisualVO();
                        projectVisualVO.setType(2);//  页
                        projectVisualVO.setId(project.getId());
                        projectVisualVO.setProjectName(project.getProjectName());
                        childen.add(projectVisualVO);
                    }
                }
            }
        }
        return groupDashboards;
    }

//    @PostMapping(value = "/getGroupByIdProject")
//    @ApiOperation("根据项目组id和用户id 获取该项目下面所有的项目")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "type", value = "仪表盘组类型  1 公共 2 个人 ", required = true, dataType = "Integer", paramType = "query")
//    })
//    public ResultVo<Object> getGroupByIdProject(@RequestParam(name = "type", required = true) Integer type) {
//        List<ProjectVisualVO> groupDashboard = panelGroupService.getGroupDashboard(type);
//        return ResultVo.success(groupDashboard);
//    }

    @PostMapping(value = "/create")
    @ApiOperation("创建仪表盘组")
    public ResultVo<Object> create(@RequestBody PanelGroupParam panelGroupParam) {
        if(panelGroupParam == null || panelGroupParam.getGroupType()<1 ||panelGroupParam.getGroupType() >2){
            return  ResultVo.error(CodeMsg.BIND_ERROR);
        }

        boolean save = panelGroupService.create(panelGroupParam);
        return save ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }

    @PostMapping(value = "/rename")
    @ApiOperation(value = "仪表盘组重命名 ",notes="传入组id:groupId 和组名称 groupName")
    public ResultVo<Object> rename(@RequestBody PanelGroupParam panelGroupParam) {
        boolean rename = panelGroupService.rename(panelGroupParam);
        return rename ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }

    @PostMapping(value = "/move")
    @ApiOperation(value = "仪表盘组移动 ",notes="")
    public ResultVo<Object> move(@RequestBody PanelGroupMoveParam panelGroupMoveParam) {
        boolean move = panelGroupService.move(panelGroupMoveParam);
        return move ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "仪表盘组删除",notes="")
    public ResultVo<Object> delete(@RequestBody PanelGroupDeleteParam panelGroupDeleteParam) {
        boolean rename = panelGroupService.delete(panelGroupDeleteParam);
        return rename ? ResultVo.success():ResultVo.error(CodeMsg.DB_ADDDATA_ERROR);
    }

}
